package com.gy.controller;

import com.gy.service.*;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
public class MyController {
    //controller层调Service层
    @Autowired
    BaoXiuService baoXiuService;
    @Autowired
    LeiXingService leiXingService;
    @Autowired
    NoticeService noticeService;
    @Autowired
    UserService userService;
    @Autowired
    QuYuService quYuService;





/*    //登录页面
    @RequestMapping({"/","/login"})
    public String toLogin() {
        return "login";
    }*/

    //登录方法
    //前端接收Username和pwd

    @RequestMapping("/login/main")
    public String Login(String username, String pwd, Model model, HttpSession session) {
        //获取当前用户
        Subject subject = SecurityUtils.getSubject();
        //封装用户的登录数据
        UsernamePasswordToken token = new UsernamePasswordToken(username, pwd);
        //查看用户权限
        String perm = userService.selectPerm(username);

        //拿到数据进行验证登录
        try {
            session.setAttribute("userperm",perm);
            //将用户名传入session中 在前端显示出来
            session.setAttribute("loginUser",username);
            subject.login(token);//这个方法就是执行登录方法  不成功会抛出异常  用户名不对  密码不对
            return "dashboard";
        } catch (UnknownAccountException e) {
            model.addAttribute("NoUser", "用户名不正确");
            return "login"; //
        } catch (IncorrectCredentialsException e) {
            model.addAttribute("NoPwd", "密码不正确");
            return "login";
        }

    }
    @RequestMapping("/main")
    public String Main(){
        return "dashboard";
    }


    //退出登录
    @RequestMapping("/all/loginout")
    public String loginout(){


        return "login";
    }


    //未授权的
    @RequestMapping("/NoAuthor")

    public String ONauthor(){

        return "NoAuthor";
    }




}
